package com.anjian.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.anjian.Constants;
import com.anjian.utils.PagingResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemUpdateResponse;

public class ActionBase extends ActionSupport implements Constants{
	
	static Logger _log = Logger.getLogger(ActionBase.class.getName());
	
	protected String getUser()
	{
		return (String)ActionContext.getContext().getSession().get(USER);
	}
	
	protected long getUserId()
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		String userId = (String)session.get(USER_ID);
		if (userId != null)
		{
			return Long.parseLong(userId);
		}
		return -1;
	}
	
	public boolean isAdmin()
	{
		return getUserId() == ADMIN_ID;
	}

	protected String getSessionId()
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		return (String)session.get(TOP_SESSION);
	}
	
	protected void resetProgress()
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		_log.info("sssssssssss: " + session);
		session.put(PROGRESS, 0);
	}
	
	protected void increaceProgress()
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		Integer progress = (Integer)session.get(PROGRESS);
		_log.info(getUser() + "progress: " + progress);
		if (progress == null)
		{
			progress = 0;
		}
		session.put(PROGRESS, ++progress);
	}
	
	protected static void error(Throwable e)
	{
		_log.log(Level.SEVERE, "", e);
	}
	
	protected void error(TaobaoResponse rsp)
	{
		_log.info(rsp.getErrorCode() + " - " + rsp.getMsg() + " - " + rsp.getSubCode() + " - " + rsp.getSubMsg());
	}
	
	protected boolean isRepeatError(String subCode)
	{
		return "isv.item-update-service-error:IC_PERMISSION_FOR_TBCP_ONLY".equals(subCode)
		|| "isv.item-update-service-error:QUANTITY_ITEM_CAT_TOO_LARGE".equals(subCode)
		|| "isv.item-update-service-error:ERROR_SHOP_CATEGORY_HAS_SUB_CATEGORIES".equals(subCode)
		|| "isv.item-update-service-error:IC_CHECKSTEP_SPU_NOT_EXIST".equals(subCode)
		|| "isv.item-update-service-error:IC_APPLICATION_PERMISSION_INVALID".equals(subCode)
		|| "isv.item-update-service-error:IC_STUFF_STATUS_NEW_LIMITED_WITH_NOPREPAY".equals(subCode);
	}
	
	protected boolean isSystemError(String subCode)
	{
		return (subCode != null && subCode.startsWith("isp."))
			|| (subCode != null && subCode.contains("isv.item-update-service-error:GENERIC_FAILURE"))
			|| "accesscontrol.limited-by-app-access-count".equals(subCode)
			|| "isv.item-is-delete:invalid-numIid-or-iid".equals(subCode)
			|| "isv.item-get-service-error:ITEM_NOT_FOUND".equals(subCode)
			|| "isv.item-update-service-error:ITEM_SALE_HAS_BID".equals(subCode);
	}
	
	protected List<Item> getItems()
	{
		PagingResult<Item> pagingItems = (PagingResult<Item>) ActionContext.getContext().getSession().get(SCANING_ITEMS);
		if (pagingItems == null)
		{
			return null;
		}
		return pagingItems.getItems();
	}
	
	protected Item getItem(int index)
	{
		List<Item> items = getItems();
		if (items != null)
		{
			return items.get(index);
		}
		return null;
	}
	
	protected List<ItemUpdateResponse> getErrorItems()
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		List<ItemUpdateResponse> errorItems = (List<ItemUpdateResponse>)session.get(ERROR_ITEMS);
		if (errorItems == null)
		{
			errorItems = new ArrayList<ItemUpdateResponse>();
			session.put(ERROR_ITEMS, errorItems);
		}
		return errorItems;
	}
}
